0258. tsc 常用参数
1. 🎯 本节内容
- 输出控制参数
- 类型检查参数
- 模块相关参数
- 调试相关参数
- 项目管理参数
2. 🫧 评价
tsc 提供了丰富的命令行参数,可以灵活控制编译行为。
- 命令行参数优先级高于配置文件
- 适合临时调整编译选项
- 可用于 CI/CD 脚本
- 不同场景需要不同参数组合
- 掌握常用参数提升开发效率
3. 🤔 输出控制参数?
3.1. --outDir
指定输出目录:
bash
# 输出到 dist 目录
tsc --outDir dist
# 输出到多级目录
tsc --outDir build/output1
2
3
4
5
2
3
4
5
text
项目结构:
src/
index.ts
utils/
helper.ts
编译后(tsc --outDir dist):
dist/
index.js
utils/
helper.js1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
3.2. --outFile
将所有文件合并输出到单个文件:
bash
# 合并输出(仅支持 AMD 和 System 模块)
tsc --outFile bundle.js --module amd1
2
2
ts
// file1.ts
export const a = 1
// file2.ts
export const b = 21
2
3
4
5
2
3
4
5
javascript
// bundle.js(合并后)
define('file1', ['require', 'exports'], function (require, exports) {
exports.a = 1
})
define('file2', ['require', 'exports'], function (require, exports) {
exports.b = 2
})1
2
3
4
5
6
7
2
3
4
5
6
7
3.3. --removeComments
移除注释:
bash
tsc --removeComments1
ts
// input.ts
/**
* 用户接口
*/
interface User {
name: string // 用户名
age: number // 年龄
}
// ✅ 编译后注释被移除1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
javascript
// output.js(--removeComments)
// 所有注释被移除1
2
2
3.4. --declaration
生成声明文件:
bash
# 生成 .d.ts 文件
tsc --declaration
# 同时生成声明映射
tsc --declaration --declarationMap1
2
3
4
5
2
3
4
5
ts
// input.ts
export function greet(name: string): string {
return `Hello, ${name}!`
}1
2
3
4
2
3
4
ts
// output.d.ts
export declare function greet(name: string): string1
2
2
4. 🤔 类型检查参数?
4.1. --strict
启用所有严格检查:
bash
tsc --strict1
等同于启用:
strictNullChecksstrictFunctionTypesstrictBindCallApplystrictPropertyInitializationnoImplicitAnynoImplicitThisalwaysStrict
4.2. --noImplicitAny
禁止隐式 any:
bash
tsc --noImplicitAny1
ts
// ❌ 会报错
function greet(name) {
// Error: Parameter 'name' implicitly has an 'any' type
return `Hello, ${name}`
}
// ✅ 正确
function greet(name: string) {
return `Hello, ${name}`
}1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
4.3. --strictNullChecks
严格空值检查:
bash
tsc --strictNullChecks1
ts
let name: string = 'Tom'
name = null // ❌ Error: Type 'null' is not assignable to type 'string'
let age: number | null = 25
age = null // ✅ 正确1
2
3
4
5
2
3
4
5
4.4. --noUnusedLocals
检查未使用的局部变量:
bash
tsc --noUnusedLocals1
ts
function calculate() {
const unused = 10 // ❌ Error: 'unused' is declared but never used
return 5
}1
2
3
4
2
3
4
4.5. --noUnusedParameters
检查未使用的参数:
bash
tsc --noUnusedParameters1
ts
// ❌ 会报错
function greet(name: string, age: number) {
// Error: 'age' is declared but never used
return `Hello, ${name}`
}
// ✅ 使用下划线表示有意忽略
function greet(name: string, _age: number) {
return `Hello, ${name}`
}1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
5. 🤔 模块相关参数?
5.1. --module
指定模块系统:
bash
# CommonJS(Node.js)
tsc --module commonjs
# ES 模块
tsc --module esnext
# AMD(浏览器,RequireJS)
tsc --module amd
# UMD(通用)
tsc --module umd1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
ts
// input.ts
export const value = 421
2
2
javascript
// CommonJS 输出
exports.value = 42
// ES 模块输出
export const value = 42
// AMD 输出
define(['require', 'exports'], function (require, exports) {
exports.value = 42
})1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
5.2. --moduleResolution
模块解析策略:
bash
# Node.js 解析
tsc --moduleResolution node
# 经典解析
tsc --moduleResolution classic1
2
3
4
5
2
3
4
5
5.3. --esModuleInterop
启用 ES 模块互操作:
bash
tsc --esModuleInterop1
ts
// ✅ 可以使用默认导入
import express from 'express' // 而不是 import * as express
// 等同于
import * as express from 'express'
const express_default = express.default || express1
2
3
4
5
6
2
3
4
5
6
5.4. --allowSyntheticDefaultImports
允许合成默认导入:
bash
tsc --allowSyntheticDefaultImports1
6. 🤔 调试相关参数?
6.1. --sourceMap
生成源码映射:
bash
tsc --sourceMap1
text
生成文件:
output.js
output.js.map ✅ 源码映射文件1
2
3
2
3
6.2. --inlineSourceMap
内联源码映射:
bash
tsc --inlineSourceMap1
javascript
// output.js
// ... 编译后的代码
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLC...1
2
3
2
3
6.3. --listFiles
列出编译的文件:
bash
tsc --listFiles1
text
输出:
/path/to/project/node_modules/typescript/lib/lib.d.ts
/path/to/project/node_modules/typescript/lib/lib.es5.d.ts
/path/to/project/src/index.ts
/path/to/project/src/utils/helper.ts1
2
3
4
5
2
3
4
5
6.4. --diagnostics
显示诊断信息:
bash
tsc --diagnostics1
text
输出:
Files: 12
Lines: 45678
Nodes: 123456
Identifiers: 34567
Symbols: 23456
Types: 5678
Instantiations: 8901
Memory used: 89012K
I/O read: 0.02s
I/O write: 0.01s
Parse time: 0.45s
Bind time: 0.23s
Check time: 1.34s
Emit time: 0.67s
Total time: 2.69s1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
7. 🤔 项目管理参数?
7.1. --project
指定配置文件:
bash
# 指定 tsconfig.json
tsc --project tsconfig.json
# 指定目录(会查找目录下的 tsconfig.json)
tsc --project ./
# 简写
tsc -p tsconfig.json1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
7.2. --build
构建项目引用:
bash
# 构建项目及其依赖
tsc --build
# 简写
tsc -b
# 强制重新构建
tsc --build --force1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
7.3. --incremental
增量编译:
bash
tsc --incremental1
7.4. --noEmit
只检查不输出:
bash
# 适用于 CI/CD 类型检查
tsc --noEmit1
2
2
7.5. --noEmitOnError
有错误时不输出:
bash
tsc --noEmitOnError1
7.6. --help
显示帮助信息:
bash
# 显示所有选项
tsc --help
# 显示完整帮助
tsc --help --all1
2
3
4
5
2
3
4
5
7.7. --version
显示版本:
bash
tsc --version
# Version 5.3.31
2
2